/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;
	
	import virtPresenter.business.MouseXMLDelegate;
	import virtPresenter.event.LoadSearchXMLEvent;
	import virtPresenter.model.MouseStep;
	import virtPresenter.model.virtPresenterLocator;	
	
	/**
	 * Commands handles a LoadMouseXMLEvent and loads the mouse.xml
	 * for the folder, given by the event.	
	 */	
	public class LoadMouseXMLCommand implements ICommand, IResponder
	{	
		/**
		 * Instance of the model.
		 */
		private var model=virtPresenterLocator.getInstance();
		private var mouseSteps:Array;
		
		/**
		 * Execute method is invoked by the LoadMouseXMLEvent. Calls
		 * its delegate to recieve the result of the mouse.xml file.
		 * 
		 * @parameters event LoadMouseXMLEvent
		 * 
		 * @see virtPresenter.event.LoadMouseXMLEvent
		 */
		public function execute(event:CairngormEvent):void
		{
			var delegate:MouseXMLDelegate = new MouseXMLDelegate(this);
			delegate.getMouseXML();					
		}
		
		/**
		 * Result method is called by the MouseXMLDelegate and stores the results
		 * of mouse.xml as an ArrayCollection in the model. 
		 * Throws a LoadLogXMLEvent.
		 *  
		 * @parameters data Data Object that contains the results of the request to the mouse.xml
		 * 
		 * @see virtPresenter.business.MouseXMLDelegate
		 */
		public function result(data:Object):void
		{	
			trace("LoadMouseXML. Mouse.xml loaded!");		
			
			try{
			model.mouseCollection = ArrayCollection(data.result.ppt_document.slide);			
			model.mouseXML = XML(data.result);
			parseMouseXML();
			model.xmlParaser.mouseXMLAvailable=true;
			}
			catch(err:Error){
				trace("LoadMouseXMLCommand. Error in parsing mouse.xml, maybe mouse.xml is empty or ill shaped: "+err.message);
				model.xmlParser.mouseXMLAvailable=false;
			}
						
			CairngormEventDispatcher.getInstance().dispatchEvent(new LoadSearchXMLEvent());				
		}
		
		/**
		 * Fault method is invoked by MouseXMLDelegate if an error occured
		 * loading the mouse.xml file.
		 * 
		 * @parameters info Info object that contains information about the fault
		 * 
		 * @see virtPresenter.business.MouseXMLDelegate  
		 */
		public function fault(info:Object):void
		{
			trace("LoadMouseXML. Fault in loading mouse.xml:"+info.toString());
			model.xmlParser.mouseXMLAvailable=false;
			CairngormEventDispatcher.getInstance().dispatchEvent(new LoadSearchXMLEvent());		
		}
		
		/**
		 * Parses the mouse.xml 
		 */
		private function parseMouseXML():void
		{			
			mouseSteps = new Array(); // Erstelle ein leeres Array
			
			for each (var item in model.mouseCollection)
			{
				var tmp:MouseStep = new MouseStep();
				
				tmp.time = int(item.time.toString());
				tmp.x = Number(item.x.toString().replace(",","."));
				tmp.y = Number(item.y.toString().replace(",","."));

				mouseSteps.push(tmp);	
			}
			
			model.xmlParser.mouseSteps=mouseSteps;			
				
		}	
		
	}
}